/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

import Database.DatabasesManager;
import Database.Table;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author PHAMDANH
 */
public class QueryPanel extends JScrollPane{
    private DatabasesManager dbManager;        
    private JPopupMenu popup;
    private JPanel panel;
    private JScrollPane topPanel;
    private JScrollPane botPanel;
    private JTable table;
    private JTextArea textArea;
    public QueryPanel(DatabasesManager dbManager)
    {
        this.dbManager = dbManager;  
        panel = new JPanel();
        topPanel = new JScrollPane();
        textArea = new JTextArea();
        textArea.setColumns(50);
        textArea.setRows(20);
        topPanel.setViewportView(textArea);
        popup = new JPopupMenu();
        popup.add(act_exe);
        textArea.add(popup);
        textArea.addMouseListener(new PopupTrigger());
        panel.add(topPanel);
        topPanel.setPreferredSize(new Dimension(300, 200));
        botPanel = new JScrollPane();
        table = new JTable();        
        botPanel.setViewportView(table);                
        panel.add(botPanel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        this.setViewportView(panel);
    }
    public void setText(String text)
    {
        textArea.setText(text);
    }
    public void ShowTable(Table result)
    {
        if(result!=null)
        {
            DefaultTableModel model = new DefaultTableModel();
            for(int i=0;i<result.getColumns().size();i++)
                model.addColumn(result.getColumns().get(i).getName());
            for(int i=0;i<result.getColumns().get(0).getData().size();i++)
            {
                Object[] row=new Object[result.getColumns().size()];
                for(int j=0;j<result.getColumns().size();j++)
                    row[j]=result.getColumns().get(j).getData().get(i);
                model.addRow(row);
            }
            table.setModel(model);
            table.repaint();
        }
        else
            JOptionPane.showMessageDialog(null, "Lỗi truy vấn", "Kết quả trả về null",JOptionPane.ERROR_MESSAGE);
    }
    private AbstractAction act_exe = new AbstractAction("Execute") 
    { 
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String str = textArea.getText();
            Object obj = dbManager.ExecuteQuery(str);
            if(obj!=null)
            {
                if(obj.getClass()==Table.class)
                {            
                    Table tb= (Table)obj;
                    ShowTable(tb);
                }
            }
        }
    };
    private class PopupTrigger extends MouseAdapter
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {            
            if (e.isPopupTrigger())
            {
                int x = e.getX();
                int y = e.getY();                                   
                popup.show(textArea, x, y);               
            }
        }
    }
}
